<?php

namespace app\modules\admin\controllers;

use app\models\AdminLoginForm;
use app\models\User;
use Yii;
use yii\web\Controller;

/**
 * Default controller for the `admin` module
 */
class SiteController extends BaseController
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->redirect('index');
        }

        $model = new AdminLoginForm();
        if($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->redirect('index');
        }
        return $this->render('login',[
            'model' => $model,
        ]);
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->redirect('/admin/site/login');
    }
}
